<?php

namespace App\Models;

use App\Service\Base\Model;


class SiteRss extends Model
{
    // 邮件订阅
    protected $table = 'site_rss';

    // 订阅
    public function onRss($email)
    {
        if(empty($email)){
            return $this->return_false('请输入邮件！');
        }

        // 查询邮件是否已经订阅
        $info = $this->isCache(true)->oneFind(['email' => $email]);
        if(!empty($info) && ($info['status']==1)){
            return $this->return_false('该邮箱已经订阅！');
        }

        // 保存邮件
        if($info){
            $data   = ['status' => 1];
            $result = $this->where('id', $info['id'])->update($data);
        }else{
            $data = ['email' => $email];
            $this->oneAdd($data);
        }
        $this->deleteCacheShow($info, $data);

        return true;
    }

    // 取消订阅
    public function cancel($email)
    {
        if(empty($email)){
            return $this->return_false('请输入邮件！');
        }

        // 查询邮件是否已经订阅
        $info = $this->isCache(true)->oneFind(['email' => $email]);
        if(empty($info)){
            return $this->return_false('该邮箱未订阅！');
        }

        // 保存邮件
        $data   = ['status' => -1];
        $result = $this->where('id', $info['id'])->update($data);

        // 缓存操作
        $this->deleteCacheShow($info, $data);

        return $result;
    }


    // 定义验证操作
    public function rules()
    {
        $add = [
            'email' => 'required',
        ];

        $edit = [
        ];

        $info = [
            //'surname'  => 'required',
            //'realname' => 'required',
        ];


        $rule = [
            //'id' => 'numeric',
            //'mobile' => 'numeric',
            //'nickname' => 'required',
            //'username' => 'required',
            'email'  => 'required',
            'status' => ['in:-1,0,1,2'],
        ];


        $msg = [
            'id'       => '自编号',
            'uid'      => '用户ID',
            'email'    => '邮箱',
            'mobile'   => '手机号码 ',
            'nickname' => '昵称 ',
            'username' => '用户名',
            'status'   => '用户状态 1-正常 0-禁用',
            'ctime'    => '创建时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add, 'info' => $info];
    }

}
